package com.robot.base.view

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.DataBindingUtil
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.LogUtils
import com.robot.base.databinding.ViewPhoneInputBinding
import com.robot.base.R

public typealias PhoneCodeResultListener = (content: String) -> Unit

class PhoneInput : ConstraintLayout {

    var phoneCodeResultListener: PhoneCodeResultListener? = null

    private var binding: ViewPhoneInputBinding =
        DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.view_phone_input, this, true)
    private val etArr: Array<EditText> = arrayOf(
        binding.etN1,binding.etN2, binding.etN3, binding.etN4
    )

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context, attrs,
        defStyleAttr
    ) {
        initView()
    }


    private fun initView() {
        etArr[0].isFocusable = true
        etArr[0].isFocusableInTouchMode = true
        etArr[0].requestFocus()
        for (i in etArr.indices) {

            etArr[i].addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(
                    s: CharSequence?,
                    start: Int,
                    count: Int,
                    after: Int
                ) {
                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                }

                override fun afterTextChanged(s: Editable?) {
//                    LogUtils.e(s,i)
                    s?.let {
                        val etS = it.toString()
                        if (etS.length == 1) {
                            if (i < etArr.size - 1) {
                                etArr[i].clearFocus()
                                etArr[i + 1].requestFocus()
                            } else {
                                KeyboardUtils.hideSoftInput(this@PhoneInput)
                                etArr[it.length - 1].clearFocus()
                            }
                        }
                        phoneCodeResultListener?.invoke(getInputResult())
                    }

                }
            })





            etArr[i].setOnKeyListener(object : OnKeyListener {
                override fun onKey(v: View?, keyCode: Int, event: KeyEvent?): Boolean {
                    if (keyCode == KeyEvent.KEYCODE_DEL && event!!.action == KeyEvent.ACTION_DOWN) {
                        etArr[i].setText("")
                        etArr[i].clearFocus()
                        if (i != 0) {
                            etArr[i - 1].requestFocus()
                        }else{
                            etArr[0].requestFocus()
                        }
                        return true
                    }
                    return false
                }
            })
        }
    }


    fun clearNoNum() {
        for (i in etArr.indices) {
            if (etArr[i].isFocused) {
                etArr[i].setText("")
                return
            }
        }
    }

    fun getInputResult(): String {
        var resultStr = ""
        for (et in etArr) {
            val s = et.text.toString()
            var add = ""
            add = if (s.isBlank()) {
                ""
            } else {
                s
            }
            resultStr += add
        }

        return resultStr
    }
}